/** ------------------------------------------------------------------------
 *	VisualGroup JavaScript prototype Classes, version 0.1
 *  (c) 2008 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/
using("Vsg.AuthorisingRule");

Vsg.AuthorisingRule.ButtonRule = Ext.extend(Vsg.AuthorisingRule,{
											
	
	_authComponents:null,
	
	/**
	 * construt the rule config
	 * @param {Object} $config
	 */											
	constructor:function($config) {
		
		Ext.apply(this , $config);
		// ---- init variable ----
		Vsg.AuthorisingRule.ButtonRule.superclass.constructor.call(this, $config);
		this.on("genComp" , this.doRule , this);
		
	},
	
	setAuthComponents:function($components) {
		this._authComponents = $components;
	},
	
	getAuthComponents:function() {
		return this._authComponents;
	},


	doRule:function($comp , $id , $sub) {
		var $authComps = this.getAuthComponents();
		try {

			var $lastCharacter = window.location.href.lastIndexOf("/");
			var $url = window.location.href.substring($lastCharacter+1);
			
			if ($authComps != undefined) {
				var $authComp = null;
				for (var $i = 0 ; $i < $authComps.length ; $i++) {
					$authComp = $authComps[$i];
					if ($id == $authComp.id && $url.indexOf($authComp.url) != -1) {
						break;
					}				
				}
				if ($authComp != null) {
					$controllerValue = $authComp.controller.value;
					var $value = eval($controllerValue);

					if ($value == Vsg.AuthorisingRule.ButtonRule.Available) {
						this._pass = $comp;
					} else if ($value == Vsg.AuthorisingRule.ButtonRule.Unavailable) {
						this._pass = null;
					} else {
						this._pass = null;
						Ext.log("定义不正常授权处理值!");
					}					
				} else {
					// --- not defined in auth component and show it by system perference ---
					this._pass = $comp; 
				}

			} else {
				this._pass = $comp;
			}
			
			
		} catch (ex) {
			throw new Vsg.AuthorisingRule.RuleException("解释权限处理出错!" , null ,Vsg.AuthorisingRule.RuleException.ParseRuleError);
			
		}
	}
});

/**
 * defined the rule xtype for component
 */
Vsg.AuthorisingRule.ButtonRule.xtype = "button";

Vsg.AuthorisingRule.ButtonRule.getMeanOfValue = function ($value) {
	var $text = "";
	switch ($value) {
		case Vsg.AuthorisingRule.ButtonRule.Unavailable:
			$text = "不可使用";
			break;
		case Vsg.AuthorisingRule.ButtonRule.Available:
			$text = "可用";
			break;
		default:
			break;
	}
	return $text;
}


Vsg.AuthorisingRule.ButtonRule.Unavailable = 0;
Vsg.AuthorisingRule.ButtonRule.Available = 1;

